//一个基于列表的应用
// 为了展示如何使用列表,我们将实现一个类似 Redbox 的影碟租赁自助查询系统。
//  读取文件的内容（模拟读取数据库）并以换行分割
// var movies = read(films.txt).split("\n");

// 防止查询时候空格的影响，去掉数组元素的空格
// function removeSpace(list) {
// 	for (var i = 0; i < list.length; i++) {
// 		list[i] = list[i].trim();
// 	}
// }
// 去掉元素的空格
// removeSpace(movies);

//  初始化数据
function initData(fileName) {
	var arr = read(fileName).split("\n");
	for (var i = 0; i < arr.length; i++) {
		arr[i] = arr[i].trim();
	}

	return arr;
}


//  定义一个客户对象
function Customer(name, movie) {
	this.name = name;
	this.movie = movie;
}

//  显示所有的movie
function displayList(list) {
	for (lit.front(); list.currPos() < list.length(); list.next()) {
		if (list.getElement() instanceof Customer) {
			print(list.getElement()['name'] + ", " + list.getElement()['movie']);
		} else {
			print(list.getElement());
		}
	}
}


// 客户查看哪些movie可以租赁
function checkOut(name, movie, filmList, customerList) {
	if (moviesList.contains(movie)) { //判断要租赁的movie是否存在
		var c = new Customer(name, movie);  //  
		customerList.append(c);  //  存放已经出租的movie
		filmList.remove(movie);  //从可出租的列表中移除
	} else {
		print(movie + " is not available.");
	}
}

//  测试
var movies = initData("films.txt");
//  使用自定义的List类，并初始化值
var moviesList = new List();
for (var i = 0; i < movies.length; i++) {
	moviesList.append(movies[i]);
}
var customerList = new List();

print("Available movies: \n");
displayList(moviesList);
putstr("\nEnter your name: ");
var name = readline();  //读取客户名字
putstr("What movie would you like ?");
var movie = readline();
checkOut(name, movie, moviesList, customerList);
print("\n Customer Rentals: \n");
displayList(customerList);
print("\nmovies now available: \n");
displayList(moviesList);

//  测试的时候复制到myListTools.js最后



